package com.scpii.universal.client;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * 本类用于对字节数组提供压缩和解压缩功�??
 * **/
public class ZipUnZip {
	/**
	 * 提供Zip压缩功能
	 * **/
	public byte[] zip(byte[] orgBytes) throws IOException {
		byte[] bytes = null;
		if (orgBytes != null && orgBytes.length > 0) {
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
			ZipEntry zipEntry = new ZipEntry("zip");
			zipEntry.setSize(orgBytes.length);
			zipOutputStream.putNextEntry(zipEntry);
			zipOutputStream.write(orgBytes);
			zipOutputStream.closeEntry();
			zipOutputStream.close();
			bytes = byteArrayOutputStream.toByteArray();
			byteArrayOutputStream.close();
		}
		return bytes;
	}

	/**
	 * 提供Zip解压缩功�??
	 * **/
	public byte[] unZip(byte[] orgBytes) throws IOException {
		byte[] bytes = null;
		if (orgBytes != null && orgBytes.length > 0) {
			ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(orgBytes);
			ZipInputStream zipInputStream = new ZipInputStream(byteArrayInputStream);
			while (zipInputStream.getNextEntry() != null) {
				byte[] buf = new byte[orgBytes.length];
				int num = -1;
				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
				while ((num = zipInputStream.read(buf, 0, buf.length)) != -1) {
					byteArrayOutputStream.write(buf, 0, num);
				}
				bytes = byteArrayOutputStream.toByteArray();
				byteArrayOutputStream.flush();
				byteArrayOutputStream.close();
			}
			zipInputStream.close();
			byteArrayInputStream.close();
		}
		return bytes;
	}
	/**
	 * 提供Gzip压缩功能
	 * **/
	public static byte[] gZip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			GZIPOutputStream gzip = new GZIPOutputStream(bos);
			gzip.write(data);
			gzip.finish();
			gzip.close();
			b = bos.toByteArray();
			bos.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/**
	 *提供Gzip的解压缩功能 
	 * **/
	public static byte[] unGZip(byte[] orgBytes) {
		byte[] b = null;
		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(orgBytes);
			GZIPInputStream gzip = new GZIPInputStream(bis);
			byte[] buf = new byte[1024];
			int num = -1;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((num = gzip.read(buf, 0, buf.length)) != -1) {
				baos.write(buf, 0, num);
			}
			b = baos.toByteArray();
			baos.flush();
			baos.close();
			gzip.close();
			bis.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	public static String bytesToHexString(byte[] bArray) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		ZipUnZip zipUnZip = new ZipUnZip();
		String string = "Hello World";
		byte[] orgBytes = string.getBytes();
		System.out.println(orgBytes.length);
		try {
			byte[] zipBytes = zipUnZip.zip(orgBytes);
			System.out.println(bytesToHexString(zipBytes));
			System.out.println(zipBytes.length);
			byte[] unZipBytes = zipUnZip.unZip(zipBytes);
			String str2 = new String(unZipBytes);
			System.out.println(str2);
			System.out.println(unZipBytes.length);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
